(function() {
  var __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  define(['handlebars', 'chaplin', 'lib/utils', 'lib/enums', 'lib/handlebars_helper'], function(Handlebars, Chaplin, utils, Enums) {
    'use strict';
    var View;
    return View = (function(_super) {
      __extends(View, _super);

      View.prototype.container = '#module-container';

      View.prototype.actionElsSelector = 'button, a, .btn, select, input, i, textarea';

      function View() {
        if (this.initialize !== View.prototype.initialize) {
          this.wrapMethod('initialize');
        }
        if (this.render !== View.prototype.render) {
          this.wrapMethod('render');
        }
        View.__super__.constructor.apply(this, arguments);
      }

      View.prototype.initialize = function(options) {
        if (options == null) {
          options = {};
        }
        View.__super__.initialize.apply(this, arguments);
        _(this).extend(options);
        if (!this.initializeIsWrapped) {
          return this.afterInitialize();
        }
      };

      View.prototype.render = function() {
        this.publishEvent('load:hide');
        View.__super__.render.apply(this, arguments);
        if (!this.renderIsWrapped) {
          return this.afterRender();
        }
      };

      View.prototype.getTemplateFunction = function() {
        var template, templateFunc;
        template = this.template;
        if (typeof template === 'string') {
          templateFunc = Handlebars.compile(template);
          this.constructor.prototype.template = templateFunc;
        } else {
          templateFunc = template;
        }
        return templateFunc;
      };

      View.prototype.wrapMethod = function(name) {
        var func, instance;
        instance = this;
        func = instance[name];
        instance["" + name + "IsWrapped"] = true;
        return instance[name] = function() {
          if (this.disposed) {
            return false;
          }
          func.apply(instance, arguments);
          instance["after" + (utils.upcase(name))].apply(instance, arguments);
          return instance;
        };
      };

      View.prototype.afterInitialize = function() {};

      View.prototype.afterRender = function() {
        this.cacheDOMElements();
        this.instanceSubviews();
        this.registryDOMEvents();
        return this.setupTooltip();
      };

      View.prototype.getError = function(resp) {
        var json;
        json = $.parseJSON(resp.responseText);
        return {
          message: json.erro.message,
          code: json.erro.code,
          status: json.status
        };
      };

      View.prototype.modelBind = function(type, handler) {
        var modelOrCollection;
        if (typeof type !== 'string') {
          throw new TypeError('View#modelBind: ' + 'type must be a string');
        }
        if (typeof handler !== 'function') {
          throw new TypeError('View#modelBind: ' + 'handler argument must be function');
        }
        modelOrCollection = this.model || this.collection;
        if (!modelOrCollection) {
          throw new TypeError('View#modelBind: no model or collection set');
        }
        modelOrCollection.off(type, handler, this);
        return modelOrCollection.on(type, handler, this);
      };

      View.prototype.modelUnbind = function(type, handler) {
        var modelOrCollection;
        if (typeof type !== 'string') {
          throw new TypeError('View#modelUnbind: ' + 'type argument must be a string');
        }
        if (typeof handler !== 'function') {
          throw new TypeError('View#modelUnbind: ' + 'handler argument must be a function');
        }
        modelOrCollection = this.model || this.collection;
        if (!modelOrCollection) {
          return;
        }
        return modelOrCollection.off(type, handler);
      };

      View.prototype.modelUnbindAll = function() {
        var modelOrCollection;
        modelOrCollection = this.model || this.collection;
        if (!modelOrCollection) {
          return;
        }
        return modelOrCollection.off(null, null, this);
      };

      View.prototype.pass = function(attribute, selector) {
        var _this = this;
        return this.modelBind("change:" + attribute, function(model, value) {
          var $el;
          $el = _this.$(selector);
          if ($el.is('input, textarea, select, button')) {
            return $el.val(value);
          } else {
            return $el.text(value);
          }
        });
      };

      View.prototype.instanceSubviews = function() {};

      View.prototype.cacheDOMElements = function() {
        this.DOMElements = {};
        this.addedToDOM = true;
        return this.DOMElements;
      };

      View.prototype.registryDOMEvents = function() {};

      View.prototype.createLoader = function() {};

      View.prototype.hideLoader = function() {};

      View.prototype.disableActions = function(label) {
        var $el;
        $el = this.$form || this.$el;
        if ($el) {
          return $el.find(this.actionElsSelector).attr('disabled', true).addClass('disabled');
        }
      };

      View.prototype.enableActions = function(label) {
        var $el;
        $el = this.$form || this.$el;
        if ($el) {
          return $el.find(this.actionElsSelector).removeAttr('disabled').removeClass('disabled');
        }
      };

      View.prototype.setupTooltip = function() {
        var $elements,
          _this = this;
        $elements = this.$('[title]');
        if ($elements.length) {
          $elements.each(function(i, el) {
            var $el, title, titleFiltered;
            $el = $(el);
            title = $el.attr('title');
            titleFiltered = Enums.messages[title] || title;
            return $el.attr('title', titleFiltered);
          });
          $elements.tooltip('destroy');
          return $elements.tooltip();
        }
      };

      View.prototype.closeTooltips = function() {
        return $('.tooltip').remove();
      };

      return View;

    })(Chaplin.View);
  });

}).call(this);
